#include "include/SessionManager.h"

using namespace std;

void SessionManager::AddSession(Session *session)
{
	int key = session->logic.server_key;
	if(_session_list.find(key) == _session_list.end())
	{
		_session_list[key] = new boost::unordered_map<unsigned int, Session *>();
	}
	else
	{
		// todo: return old session, notify player kick off
	}

	(*_session_list[key])[session->logic.session_key] = session;
}

void SessionManager::DelSession(Session *session)
{
	int key = session->logic.server_key;

	if(_session_list.find(key) == _session_list.end())
	{
		return;
	}

	_session_list[key]->erase(session->logic.session_key);
}

Session * SessionManager::GetSession(int server_key, unsigned int session_key)
{
	if(_session_list.find(server_key) == _session_list.end())
	{
		return NULL;
	}

	return (*_session_list[server_key])[session_key];
}

Session *SessionManager::GetFirstSession(int key)
{
	if(_session_list.find(key) == _session_list.end())
	{
		return NULL;
	}

	_current_key = key;
	_iterator = _session_list[key]->begin();
	return _iterator->second;
}

Session *SessionManager::GetNextSession()
{
	_iterator++;
	if(_session_list[_current_key]->end() == _iterator)
	{
		return NULL;
	}

	return _iterator->second;
}
